﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using SPMS.Common;
using SPMS.Data;
using SPMS.Web.Models;
using SPMS.Web.Mvc;

namespace SPMS.Web.Controllers
{
    [AuthorizeEx]
    public class ActionTypeController : Controller
    {
        public ActionResult Action()
        {
            return View();
        }

        [HttpPost]
        [JsonHandleError]
        public ActionResult Create(ActionTypeModel model)
        {
            using (var context = new AccountFileManagementEntities())
            {
                if (context.ActionTypes.Any(item => item.ActionType_Name == model.ActionType_Name))
                    return this.JsonEx(false, string.Format("名为'{0}'的操作已经存在，操作名不允许重复。<br/>请将操作名指定为其它名称。", model.ActionType_Name));

                ActionType entity = new ActionType()
                {
                    ActionType_ID = Guid.NewGuid(),
                    ActionType_Code = model.ActionType_Code,
                    ActionType_Name = model.ActionType_Name,
                    Memo = model.Memo
                };

                context.ActionTypes.Add(entity);
                context.SaveChanges();
                return this.JsonEx(true, entity.ActionType_ID);
            }
        }

        [HttpPost]
        [JsonHandleError]
        public ActionResult Delete(ActionTypeModel model)
        {
            if (null == model)
                return this.JsonEx(false, "无效的数据请求！");

            //创建上下文
            using (var context = new AccountFileManagementEntities())
            {
                ActionType entity = context.ActionTypes.FirstOrDefault(item => item.ActionType_ID == model.ActionType_ID);

                if (null != entity)
                {
                    context.ActionTypes.Remove(entity);
                    context.SaveChanges();
                }

                //返回结果
                return this.JsonEx();
            }
        }

        [HttpGet]
        [JsonHandleError]
        public ActionResult Detail(ActionTypeQueryModel model)
        {
            using (var context = new AccountFileManagementEntities())
            {
                //查询数据
                var entity = context.ActionTypes.FirstOrDefault(item => item.ActionType_ID == model.ActionType_ID);

                //创建数据
                var data = new
                {
                    entity.ActionType_ID,
                    entity.ActionType_Code,
                    entity.ActionType_Name,
                    entity.Memo
                };

                //返回数据
                return this.JsonEx(true, data, 1);
            }
        }

        [HttpPost]
        [JsonHandleError]
        public ActionResult Update(ActionTypeModel model)
        {

            using (var context = new AccountFileManagementEntities())
            {
                if (context.ActionTypes.Any(item => item.ActionType_Name == model.ActionType_Name && item.ActionType_ID != model.ActionType_ID))
                    return this.JsonEx(false, string.Format("名为'{0}'的操作已经存在，操作名不允许重复。<br/>请将操作名指定为其它名称。", model.ActionType_Name));

                ActionType entity = context.ActionTypes.FirstOrDefault(item => item.ActionType_ID == model.ActionType_ID);

                if (null == entity)
                    return this.JsonEx(false, string.Format("名为'{0}'的操作不存在，无法更新。<br/>请确认此操作是否已经被删除。", model.ActionType_ID));

                if (null != entity)
                {
                    entity.ActionType_Code = model.ActionType_Code;
                    entity.ActionType_Name = model.ActionType_Name;
                    entity.Memo = model.Memo;
                    context.SaveChanges();
                }

                return this.JsonEx();
            }
        }

        [HttpGet]
        [JsonHandleError]
        public JsonResult List(JsonPagingQueryModel queryModel)
        {
            //创建上下文
            using (var context = new AccountFileManagementEntities())
            {
                //用于保存总记录数
                int totalRecord = 0;

                //查询数据
                var query = from item in context.ActionTypes.QueryData<ActionType>(queryModel, out totalRecord)
                            select new
                            {
                                item.ActionType_ID,
                                item.ActionType_Code,
                                item.ActionType_Name,
                                item.Memo
                            };

                //获得数据
                var data = query.ToArray();

                return this.JsonEx(true, data, totalRecord);
            }
        }
    }
}
